package trlygjj.tanruiliyigenjinjin.web.controller;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.Employee;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.EmployeeQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IDepartmentService;
import trlygjj.tanruiliyigenjinjin.service.IEmployeeService;
import trlygjj.tanruiliyigenjinjin.util.ExcelException;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.io.File;
import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("systemEmployee")
public class SystemEmployeeController {

    @Autowired
    private IEmployeeService systemEmployeeService;

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     *
     * @return
     */
    @RequiresPermissions("systemEmployee:index")
    @PermissionName("系统员工主页")
    @RequestMapping("")
    public String index() {
        return "systemEmployee/list";
    }

    /**
     * 具有分页和高级查询
     *
     * @return
     */
    @RequiresPermissions("systemEmployee:list")
    @PermissionName("系统员工数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(EmployeeQueryObject qo) {
        PageResult result = systemEmployeeService.query(qo);
        return result;
    }

    @RequiresPermissions("systemEmployee:resetPassword")
    @PermissionName("重置密码")
    @RequestMapping("resetPassword")
    @ResponseBody
    public JsonResult resetPassword(Long id, String username) {
        JsonResult result = new JsonResult();
        try {
            systemEmployeeService.resetPassword(id, username);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }


    @RequestMapping("changeAdmin")
    @RequiresPermissions("systemEmployee:changeAdmin")
    @PermissionName("授权管理员状态")
    @ResponseBody
    public JsonResult changeAdmin(Long id) {
        JsonResult result = new JsonResult();
        try {
            systemEmployeeService.changeAdmin(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

    @RequiresPermissions("systemEmployee:exportExcel")
    @PermissionName("系统员工导出功能")
    @RequestMapping("exportExcel")
    @ResponseBody
    public JsonResult exportExcel() throws Exception{
        JsonResult result = new JsonResult();
        try {
            List<Employee> employees = systemEmployeeService.selectAll();
            //创建工作簿
            WritableWorkbook workbook = Workbook.createWorkbook(new File("e:/exportExcel.xls"));
            WritableSheet sheet = workbook.createSheet("系统员工表", 0);
            //设置样式
            WritableFont wf2   =   new   WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
            WritableCellFormat wcfTitle = new WritableCellFormat(wf2);
            wcfTitle.setBackground(jxl.format.Colour.IVORY);  //象牙白
            wcfTitle.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle边框
            wcfTitle.setAlignment(Alignment.CENTRE); //设置垂直对齐

            CellView navCellView = new CellView();
            navCellView.setAutosize(true); //设置自动大小

            String[] heads = {"账号名", "职位", "电话", "邮箱","部门","入职时间","管理员","状态"};
            Label label = null;
            for(int i=0;i<heads.length;i++){
                sheet.setColumnView(i, navCellView);//根据内容自动设置列宽
                label = new Label(i, 0, heads[i],wcfTitle);
                sheet.addCell(label);
            }
            for (int i =1;i<=employees.size();i++){

                label = new Label(0, i, employees.get(i-1).getUsername(),wcfTitle);
                sheet.addCell(label);
                label = new Label(1, i, employees.get(i-1).getPositions(),wcfTitle);
                sheet.addCell(label);
                label = new Label(2, i, employees.get(i-1).getTel(),wcfTitle);
                sheet.addCell(label);
                label = new Label(3, i, employees.get(i-1).getEmail(),wcfTitle);
                sheet.addCell(label);
                label = new Label(4, i, employees.get(i-1).getDepartment().getName(),wcfTitle);
                sheet.addCell(label);
                label = new Label(5, i, employees.get(i-1).getInputTime().toString(),wcfTitle);
                sheet.addCell(label);
                if (employees.get(i-1).getAdmin()==null){

                    label = new Label(6, i, "否",wcfTitle);
                    sheet.addCell(label);
                }
                else {
                    label = new Label(6, i, employees.get(i-1).getAdmin()==0?"否":"是",wcfTitle);
                    sheet.addCell(label);
                }

                if(employees.get(i-1).getState()==null){
                    label = new Label(7, i, "离职",wcfTitle);
                    sheet.addCell(label);
                }else {
                    label = new Label(7, i, employees.get(i-1).getState()==0?"离职":"在职",wcfTitle);
                    sheet.addCell(label);
                }
            }
            workbook.write();
            workbook.close();
        }catch (ExcelException e){
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

}
